const express = require("express");
const app = express();
app.all("*", function (req, res, next) {
  //设为指定的域
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Headers", "Content-Type");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("Access-Control-Allow-Credentials", true);
  res.header("X-Powered-By", " 3.2.1");
  next();
});

//设置body解析中间件
app.use(express.json()); ////数据JSON类型
app.use(express.urlencoded({ extended: false })); //解析post请求数据
app.get("/", function (req, res, next) {
  console.log("我被get请求了");
  res.send("Hello World!");
  next();
});
let info = [
  {
    date: "2016-05-02",
    name: "王棒槌",
    address: "上海市普陀区金沙江路 1518 弄",
  },
  {
    date: "2016-05-04",
    name: "王小虎",
    address: "上海市普陀区金沙江路 1517 弄",
  },
  {
    date: "2016-05-01",
    name: "毛丹丹",
    address: "海口 1519 弄",
  },
  {
    date: "2016-05-03",
    name: "王小虎",
    address: "上海市普陀区金沙江路 1516 弄",
  },
  {
    date: "2022-10-19",
    name: "王大吃",
    address: "贝佳斯弄",
  },
  {
    date: "2016-01-03",
    name: "李大叔",
    address: "上海市普陀区金沙江路 1519 弄",
  },
  {
    date: "2015-01-03",
    name: "信息科",
    address: "疏导疏导",
  },
  {
    date: "2015-01-03",
    name: "就开了",
    address: "安安定定",
  },
  {
    date: "2088-01-03",
    name: "辅导费",
    address: "三大发的",
  },
  {
    date: "2097-01-03",
    name: "第三方斯蒂芬",
    address: "辅导费第三方",
  },
  {
    date: "1015-01-03",
    name: "去去去",
    address: "佛挡杀佛",
  },
  {
    date: "2099-01-03",
    name: "阿萨德",
    address: "滚滚滚",
  },
  {
    date: "2755-01-02",
    name: "话费",
    address: "虎虎生风",
  },
  {
    date: "2333-01-03",
    name: "去撒",
    address: "花港饭店",
  },
  {
    date: "2024-01-03",
    name: "发顺丰",
    address: "公司发顺丰",
  },
  {
    date: "2023-01-03",
    name: "很广泛",
    address: "看见好",
  },
  {
    date: "7830-01-03",
    name: "发送的",
    address: "为地址",
  },
  {
    date: "2005-01-03",
    name: "规范到位",
    address: "青春啊",
  },
  {
    date: "9822-01-03",
    name: "如果他",
    address: "就一天",
  },
  {
    date: "8933-01-03",
    name: "分工会尽快",
    address: "巨化股份",
  },
];
let total = info.length;
info = info.map((item, index) => ({
  ...item,
  indey: index + 1,
  indez: Math.floor(Math.random().toFixed(2) * 100),
}));
const startWith = (key, value) => {
  return new RegExp("^" + key + "\\S*").test(value);
};
const endWith = (key, value) => {
  return new RegExp("\\S" + key + "$").test(value);
};
app.post("/user/info", function (req, res, next) {
  const { pageNum, pageSize, orderBy, direction, filters, conditions } =
    req.body;
  let data = JSON.parse(JSON.stringify(info));
  if (conditions && conditions.length) {
    conditions.map(({ key, op, value }) => {
      data = data.filter((item) => {
        if (value) {
          if (op === "eq") {
            return item[key] === value;
          } else if (op === "ne") {
            return item[key] !== value;
          } else if (op === "in") {
            if (Array.isArray(value)) {
              return value.includes(item[key]);
            }
          } else if (op === "not-in") {
            if (Array.isArray(value)) {
              return !value.includes(item[key]);
            }
          } else if (op === "lt") {
            return item[key] < value;
          } else if (op === "le") {
            return item[key] <= value;
          } else if (op === "gt") {
            return item[key] > value;
          } else if (op === "ge") {
            return item[key] >= value;
          } else if (op === "like") {
            return item[key] === value || item[key].includes(value);
          } else if (op === "start_with") {
            return startWith(value, item[key]) || item[key] === value;
          } else if (op === "end_with") {
            return endWith(value, item[key]) || item[key] === value;
          }
        } else {
          return item;
        }
      });
    });
  }
  total = data.length;
  if (orderBy && direction) {
    data.sort((a, b) => {
      if (direction === "ascending") {
        return a[orderBy] - b[orderBy];
      } else {
        return b[orderBy] - a[orderBy];
      }
    });
  }
  data = data.slice((pageNum - 1) * pageSize, pageNum * pageSize);
  res.send({
    total: total,
    data,
    code: 200,
  });
  next();
});
app.listen(8001, () => {
  console.log("服务器启动成功", 8001);
});
